<template>
  <div>
    <!--条件筛选-->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      :onColumnsChange="columns => this.columns = columns"
      :cellClick="cellClick"
      :cellstyles="cellstyles"
      :onSyncTableData="tableChangeFun"
      :isExportExcel2="true"
      :exportFileName="customExportFileName"
    >
    </FilterTable>

    <BaseDialog
      :visible.sync="issearch"
      :title="'服务商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher  @close="dealercloseHandler" />
    </BaseDialog>

    <Drawer
      :visible.sync="openInvoiceDetail"
      :title="'交货单明细'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <invoiceDetail :params="invoiceParams" @close="invoiceDetailCloseHandler" />
    </Drawer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import {getInvoice} from '@/api/partsmanage/financialManagement/invoice/index';
import ownerSeacher from '@/pages/partsmanage/claim/approve/ownerSeacher';
import invoiceDetail from '@/pages/partsmanage/financialManagement/invoice/customPopup/invoiceDetail';
import moment from 'moment';
export default {
  name: "invoiceQuery",
  mixins: [dictionary],
  components: {ownerSeacher,invoiceDetail},
  data() {
    return{
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: getInvoice,
      issearch: false,
      openInvoiceDetail: false,
      invoiceParams:{},
      fetchParams: {xhrAbort: true},
      customExportFileName:'备件发票信息'+moment().format("YYYY-MM-DD")+'.xlsx',
      cellstyles:[]
    };
  },
  async created() {
  },
  methods: {
    tableChangeFun(dataList) {
      dataList.forEach((item, index) => {
        this.cellstyles.push({ row: item, dataIndex: 'bsysdh', styles: { color: '#fa8c4b' } });
      });
    },
    invoiceDetailCloseHandler(){
      this.openInvoiceDetail = false;
      // 重新加载
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    //当用户点击交货单号时弹框看明细
    cellClick(row,cellName,column,cell,event){
      if(cellName == "bsysdh"){
        this.openInvoiceDetail = true;
        this.invoiceParams = row;
      }
    },
    createTopFilterList() {
      return[
        {
          type: 'INPUT',
          label: '服务商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '服务商简称',
          fieldName: 'dealerName',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '备件订单号',
          fieldName: 'xsddhm',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '交货单号',
          fieldName: 'shipmentNo',
          placeholder: '请输入',
        },        
        {
          type: 'INPUT',
          label: 'SAP票号',
          fieldName: 'invoiceNoSap',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '发票号',
          fieldName: 'invoiceNo',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '是否被红冲',
          fieldName: 'whetherTheHedge',
          placeholder: '请输入',
          itemList: this.createDictList('1004'),
        },
        {
          type: 'SELECT',
          label: '是否作废',
          fieldName: 'isInvalid',
          placeholder: '请输入',
          itemList: this.createDictList('1004'),
        },
        {
          type: 'INPUT',
          label: '关联发票',
          fieldName: 'invoiceNoHedge',
          placeholder: '请输入',
        },
        {
          type: 'RANGE_DATE',
          fieldName: 'invoiceStartDate|invoiceEndDate',
          label: '开票日期',
          initialValue: [],
          dateType: 'daterange'
        },
      ]
    },
    createTableColumns () {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          align:"center",
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '服务商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          align:"center"
        },
        {
          title: '服务商简称',
          dataIndex: 'dealerName',
          sorter: true,
          align:"center"
        },
        {
          title: '备件订单号',
          dataIndex: 'xsddhm',
          sorter: true,
          align:"center"
        },
        {
          title: '交货单号',
          dataIndex: 'bsysdh',
          sorter: true,
          align:"center"
        },
        {
          title: '出库数量',
          dataIndex: 'bsfysl',
          sorter: true,
          align:"center"
        },
        {
          title: '出库金额',
          dataIndex: 'bsfyje',
          sorter: true,
          precision: 2,
          align:"center",
          numberFormat: true
        },
        {
          title: 'SAP票号',
          dataIndex: 'invoiceNoSap',
          sorter: true,
          align:"center"
        },
        {
          title: '发票号',
          dataIndex: 'invoiceNo',
          sorter: true,
          align:"center"
        },
        {
          title: '开票金额',
          dataIndex: 'invoiceAmount',
          sorter: true,
          precision: 2,
          align:"center",
          numberFormat: true
        },
        {
          title: '是否被红冲',
          dataIndex: 'whetherTheHedge',
          sorter: true,
          align:"center",
          dictItems:this.createDictList('1004'),
        },
        {
          title: '是否作废',
          dataIndex: 'isInvalid',
          sorter: true,
          align:"center",
          dictItems:this.createDictList('1004'),
        },
        {
          title: '关联发票',
          dataIndex: 'invoiceNoHedge',
          sorter: true,
          align:"center"
        },
        {
          title: '开票日期',
          dataIndex: 'invoiceDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          align:"center"
        },
        {
          title: '创建人',
          dataIndex: 'invoiceByName',
          sorter: true,
          align:"center"
        },
        {
          title: '备注',
          dataIndex: 'invoiceRemark',
          sorter: true,
          align:"center"
        }
      ]
    },
    seacherFun(){
      this.issearch = true
    },
    dealercloseHandler(val){
      if(val){
        this.topFilterList.find(x=>x.fieldName=='dealerCode').initialValue=val.dealerCode;
        this.topFilterList.find(x=>x.fieldName=='dealerName').initialValue=val.dealerShortname;
      }
      this.issearch=false;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
  }
}
</script>

<style lang="less" scoped>
.red {
  color: red !important;
}

.orange {
  color: orange;
}

.yellow {
  color: yellow;
}

/deep/.table-wrapper .el-table__body tbody>tr>td .cell{color: inherit !important;}
</style>
